ref(nextjs): Make build-phase check more robust #5857
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This makes a few changes to the
isBuild
utility function in the nextjs SDK in order to make its operation less brittle. Currently, it relies on being called at the top level ofindex.server.ts
, which ensures that it runs early enough in the build process to be executed in the main thread, before operation has been split out into child processes. (This is important because child processes don't conveniently have the word 'build' in their invocations the way the main thread (invoked bynext build
) does, removing one ofisBuild
's clues about what phase it's in.) It then sets an environment variable as a clue to future calls to itself, so that, child processes or not, it knows the correct phase. This means, however, that if it stops being used atindex.server.ts
's top level, and doesn't get called until after the process split, it won't have any way to know whether it's in the build phase or not.This makes two changes to guard against those possibilities:
NEXT_PHASE
environment variable. (The reason it doesn't simply rely on this variable to begin with is that it's only set partway through the build process.)As a result of these changes, two other changes are included here:
isBuild
is deprecated in favor of the computed constant. (Since they're equivalent, no reason to keep exporting them both.)isVercel
has been renamedIS_VERCEL
, to matchIS_BUILD
. (Yes, I'm that kind of OCD.)